package com.servlet;

import com.bean.*;
import com.dao.BookDao;
import com.dao.HistoryDao;
import com.utils.PageUtil;
import com.utils.ResponseUtils;
import com.utils.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @ClassName HistoryServlet
 * @Desc 前台历史数据控制层
 * @Author xiaoding
 * @Date 2021/1/16 21:31
 * @Version 1.0
 */
@WebServlet(name = "HistoryServlet",value = "/HistoryServlet")
public class HistoryServlet extends HttpServlet {
    private HistoryDao historyDao = new HistoryDao();
    private BookDao bookDao = new BookDao();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取参数，判断功能属性
        String str = request.getParameter("flag");

        //选择功能属性
        if (str.equals("selectBorrow")) {
            //设置当前页
            int page = 1;
            //判断是否是第一次查询
            if (request.getParameter("page") != null) {
                page = Integer.parseInt(request.getParameter("page"));
            }
            //定义状态
            String flag = "已借阅";

            //获取用户信息
            User user = (User) request.getSession().getAttribute("user");
            //查询所有借阅图书的数量
            int count = historyDao.historyCount(new HistoryReturn(user.getuId(),flag));
            //定义访问的Servlet
            String targetUrl = "HistoryServlet";
            //定义参数
            String param = "flag=selectBorrow";
            //调用获取分页的方法
            String pageUtil = PageUtil.genPagination(targetUrl, count, page, PageUtil.pageSize, param);
            //调用查询数据的方法
            List<History> history = historyDao.historySelect(new HistoryReturn(user.getuId(), flag ,page,PageUtil.pageSize));

            //设置会话
            request.setAttribute("history",history);
            request.setAttribute("pageUtil",pageUtil);

            //跳转页面
            request.getRequestDispatcher("pre/bookLending.jsp").forward(request, response);
        }else if (str.equals("selectReturn")) {
            //设置当前页
            int page = 1;
            //判断是否是第一次查询
            if (request.getParameter("page") != null) {
                page = Integer.parseInt(request.getParameter("page"));
            }
            //定义状态
            String flag = "已归还";

            //获取用户信息
            User user = (User) request.getSession().getAttribute("user");
            //查询所有借阅图书的数量
            int count = historyDao.historyCount(new HistoryReturn(user.getuId(),flag));
            //定义访问的Servlet
            String targetUrl = "HistoryServlet";
            //定义参数
            String param = "flag=selectReturn";
            //调用获取分页的方法
            String pageUtil = PageUtil.genPagination(targetUrl, count, page, PageUtil.pageSize, param);
            //调用查询数据的方法
            List<History> history = historyDao.historySelect(new HistoryReturn(user.getuId(), flag ,page,PageUtil.pageSize));

            //设置会话
            request.setAttribute("history",history);
            request.setAttribute("pageUtil",pageUtil);

            //跳转页面
            request.getRequestDispatcher("pre/bookReturn.jsp").forward(request, response);
        }else if (str.equals("update")) {
            //获取借阅记录的Id号
            int hId = Integer.parseInt(request.getParameter("hId"));
            //设置状态
            String flag = "已归还";
            //设置状态、路径
            String message = null;
            String url = null;
            //设置当前页
            int page = 1;
            //判断是否是第一次查询
            if (StringUtils.isNotNull(request.getParameter("page"))) {
                page = Integer.parseInt(request.getParameter("page"));
            }

            //调用归还书籍的方法
            int a = historyDao.historyReturn(new HistoryReturn(flag,hId));
            //判断是否归还成功
            if (a > 0) {
                message = "归还成功";
            }else {
                message = "归还失败";
            }

            //设置搜索名称
            String bName = null;
            //判断是否是默认查询或根据名称查询
            if (StringUtils.isNotNull(request.getParameter("bName"))) {
                bName = request.getParameter("bName");
                url = "HistoryServlet?flag=searchBorrow&page="+page+"&bName="+bName;
            }else {
                url = "HistoryServlet?flag=selectBorrow&page="+page;
            }
            //跳转页面
            ResponseUtils.writeMessage(response,message,url);
        }else if (str.equals("add")) {
            //获取图书编号
            int bId = Integer.parseInt(request.getParameter("bId"));
            //获取用户信息
            User user = (User) request.getSession().getAttribute("user");
            //状态
            String flag = "已借阅";
            //设置状态、路径
            String message = null;
            String url = null;
            //设置当前页
            int page = 1;

            //判断书籍是否以借阅
            if (historyDao.isBookBorrow(new HistoryReturn(user.getuId(), bId, flag)) == null) {
                //根据Id查询，图书信息
                Book book = bookDao.bookSelectId(bId);
                //借阅时间
                Date date1 = new Date();
                //归还时间
                Calendar calendar = Calendar.getInstance();
                //设置时间
                calendar.setTime(date1);
                //增加天数
                calendar.add(Calendar.DATE,user.getCount());

                //调用添加历史的方法
                int a = historyDao.addHistory(new History(user.getuId(), user.getuName(),book.getbId(), book.getbName(), flag,date1,calendar.getTime()));

                //判断是否是第一页
                if (StringUtils.isNotNull(request.getParameter("page"))) {
                    page = Integer.parseInt(request.getParameter("page"));
                }

                //设置搜索名称
                String bName = null;
                //判断是否借阅成功
                if (a > 0) {
                    message = "借阅成功";
                }else {
                    message = "借阅失败";
                }

                //判断是否是默认查询或根据名称查询
                if (StringUtils.isNotNull(request.getParameter("bName"))) {
                    bName = request.getParameter("bName");
                    url = "BookServlet?flag=search&page="+page+"&bName="+bName;
                }else {
                    url = "BookServlet?flag=select&page="+page;
                }
            }else {
                message = "当前图书您已借阅，不能重复借阅";
                url = "BookServlet?flag=select&page="+page;
            }
            //调用跳转的方法
            ResponseUtils.writeMessage(response,message,url);
        }else if (str.equals("searchBorrow")) {
            //获取图书名称
            String bName = request.getParameter("bName");
            //设置当前页
            int page = 1;
            //判断是否是第一次查询
            if (request.getParameter("page") != null) {
                page = Integer.parseInt(request.getParameter("page"));
            }
            //获取用户信息
            User user = (User) request.getSession().getAttribute("user");
            //设置状态
            String flag = "已借阅";

            //查询所有借阅图书的数量
            int count = historyDao.historySearchCount(new HistoryReturn(user.getuId(), "%" + bName + "%", flag));
            //定义访问的Servlet
            String targetUrl = "HistoryServlet";
            //定义参数
            String param = "flag=searchBorrow&bName="+bName;
            //调用获取分页的方法
            String pageUtil = PageUtil.genPagination(targetUrl, count, page, PageUtil.pageSize, param);
            //调用查询数据的方法
            List<History> history = historyDao.historySearch(new HistoryReturn(user.getuId(),"%" + bName + "%",flag,page,PageUtil.pageSize));

            //设置会话
            request.setAttribute("history",history);
            request.setAttribute("pageUtil",pageUtil);

            //跳转页面
            request.getRequestDispatcher("pre/bookLending.jsp?bName="+bName).forward(request, response);
        }else if (str.equals("searchReturn")) {
            //获取图书名称
            String bName = request.getParameter("bName");
            //设置当前页
            int page = 1;
            //判断是否是第一次查询
            if (request.getParameter("page") != null) {
                page = Integer.parseInt(request.getParameter("page"));
            }
            //获取用户信息
            User user = (User) request.getSession().getAttribute("user");
            //设置状态
            String flag = "已归还";

            //查询所有借阅图书的数量
            int count = historyDao.historySearchCount(new HistoryReturn(user.getuId(), "%" + bName + "%", flag));
            //定义访问的Servlet
            String targetUrl = "HistoryServlet";
            //定义参数
            String param = "flag=searchReturn&bName="+bName;
            //调用获取分页的方法
            String pageUtil = PageUtil.genPagination(targetUrl, count, page, PageUtil.pageSize, param);
            //调用查询数据的方法
            List<History> history = historyDao.historySearch(new HistoryReturn(user.getuId(),"%" + bName + "%",flag,page,PageUtil.pageSize));

            //设置会话
            request.setAttribute("history",history);
            request.setAttribute("pageUtil",pageUtil);

            //跳转页面
            request.getRequestDispatcher("pre/bookReturn.jsp").forward(request, response);
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
